package com.qfedu.aunt.commons.slot.impl;

import com.alibaba.druid.util.StringUtils;
import com.qfedu.aunt.commons.constance.RedisConstance;
import com.qfedu.aunt.commons.exception.BusinessException;
import com.qfedu.aunt.commons.manager.RedisManager;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.slot.base.BaseSlot;
import com.qfedu.aunt.commons.utils.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiaobobo
 * @title: BlackSlot
 * @projectName aunt_commons_2209
 * @description: TODO
 * @date 2022/11/4  15:58
 */
@Component
@Slf4j
public class BlackSlot implements BaseSlot {

    @Autowired
    private RedisManager redisManager;

    @Override
    public void dealReq(HttpServletRequest req) {
        log.info("执行到了黑名单之中...");
        /**
         * 使用String类型来设计黑名单
         * key=black:ip地址   随机值
         */
        //首先更具传递的ip获取这个数据看是否存在
        String black = (String) redisManager.getString(RedisConstance.getBLACK() + IpUtils.getIpAddress(req));
        if(!StringUtils.isEmpty(black)){
            throw new BusinessException(ResponseCode.BLACK_EXCEPTION.getCode(),
                    ResponseCode.BLACK_EXCEPTION.getMessage());
        }

        log.info("黑名单校验通过...");
    }
}
